<template>
  <div class="star star-24">
    <span class="star-item" :class="item" v-for="(item, index) in getStars" :key="index"></span>
  </div>
</template>

<script>
export default {
  props: {
    rating: {
      required: true,
      type: Number,
      default: 5
    }
  },
  computed: {
    getStars() {
      let stars = []
      // 向下取证
      let rating = this.rating
      let onClass = Math.floor(rating)

      // 1. 全星
      for (let i = 0; i < onClass; i++) {
        stars.push('on')
      }

      // 2. 半星
      (rating - onClass > 0) && stars.push('half')
      
      // 3. 灰色星星
      while(stars.length < 5) {
        stars.push('off')
      }

      return stars
    }
  }
}
</script>

<style lang="stylus">

</style>